package com.guaju.yueshangLife.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.bean.CategoryTabBean;
import com.guaju.yueshangLife.bean.CategoryTotal;
import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoLinearLayout;

public class ItemCategoryTab extends AutoLinearLayout {

    private LayoutInflater layoutInflater;
    private ImageView category_tab_iv;
    private TextView category_tab_tv;
    private String tag_name;
    private String iconUrl;

    public ItemCategoryTab(Context context) {
        super(context);
        init();
    }

    public ItemCategoryTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.category_item);
        int resourceId = typedArray.getResourceId(R.styleable.category_item_icon, R.mipmap.loadding);
        tag_name = typedArray.getString(R.styleable.category_item_name);
        if (!TextUtils.isEmpty(tag_name)) {
            category_tab_tv.setText(tag_name);
        }
        category_tab_iv.setBackgroundResource(resourceId);
    }

    public ItemCategoryTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        layoutInflater = LayoutInflater.from(getContext());
        View view = layoutInflater.inflate(R.layout.item_category_tab, this);
        category_tab_iv = (ImageView) view.findViewById(R.id.category_tab_iv);
        category_tab_tv = (TextView) view.findViewById(R.id.category_tab_tv);
    }

    public void setIconAndTitle(CategoryTabBean bean) {
        String title = bean.getTitle();
        int resId = bean.getResId();
        String iconUrl = bean.getIconUrl();
        this.iconUrl = iconUrl;
        if (!TextUtils.isEmpty(title)) {
            category_tab_tv.setText(title);
        }
        if (0 != resId) {
            category_tab_iv.setBackgroundResource(resId);
        }
        if (!TextUtils.isEmpty(iconUrl)) {
            Glide.with(getContext()).load(iconUrl).into(category_tab_iv);
        }
    }

    public void setIcon(String iconUrl) {
        this.iconUrl = iconUrl;
        if (!TextUtils.isEmpty(iconUrl)) {
            Glide.with(getContext()).load(iconUrl).centerCrop().into(category_tab_iv);
        }
    }

    public void setIcon(CategoryTotal.DataBean.ChildrenBeanX.ChildrenBean child) {
        if (child != null) {
            if (tag_name.startsWith(child.getName())) {
                CategoryTotal.DataBean.ChildrenBeanX.ChildrenBean.CoverBeanXX cover = child.getCover();
                String iconUrl = cover.getHost() + cover.getPath();
                this.iconUrl = iconUrl;
                setIcon(iconUrl);
            }
        }

    }

    public String getIconUrl() {
        return iconUrl;
    }


}
